package com.example.safeassistan.home.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;


import com.example.safeassistan.R;
import com.example.safeassistan.utile.WeekUtils;
import com.example.safeassistan.widget.CalendarView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

/**
 * 日期选择dialog
 */
public class DateSelectDialog extends Dialog  {

    private Context context;
    private CalendarView calendarYear,calendarDay,calendarMonth;
    private String year="",month="",day="",title="";
    private TextView tvTitle,conform_tv;

    public DateSelectDialog(Context context,String title) {
        super(context, R.style.custom_dialog_style);
        this.context=context;
        this.title=title;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        setContentView(R.layout.dialog_date_select);
        calendarYear=findViewById(R.id.calendarYear);
        calendarMonth=findViewById(R.id.calendarMonth);
        calendarDay=findViewById(R.id.calendarDay);
        conform_tv=findViewById(R.id.conform_tv);
        tvTitle=findViewById(R.id.tvTitle);
        tvTitle.setText(title);
        conform_tv.setOnClickListener(new clickListener());
        //定义滚动选择器的数据项（年月日的）
        ArrayList<String> gradeYear = new ArrayList<>();
        ArrayList<String> gradeMonth = new ArrayList<>();
        ArrayList<String> gradeDay = new ArrayList<>();

        //为数据项赋值
        int thisYear = Integer.parseInt(new SimpleDateFormat("yyyy").format(System.currentTimeMillis()));
       int thisMonth= Integer.parseInt(new SimpleDateFormat("MM").format(System.currentTimeMillis()));
       int thisDay=Integer.parseInt(new SimpleDateFormat("DD").format(System.currentTimeMillis()));

       year=thisYear+"";
       month=thisMonth+"";
       day=thisDay+"";

        for(int i=1980;i<=thisYear+10;i++) //从1980到今年
            gradeYear.add(i + "年");
        for(int i=1;i<=12;i++)            // 1月到12月
            gradeMonth.add(i + "月");
        for(int i=1;i<=31;i++)           // 1日到31日
            gradeDay.add(i + "日");

        //为滚动选择器设置数据
        calendarYear.setData(gradeYear);
        calendarMonth.setData(gradeMonth);
        calendarDay.setData(gradeDay);


        for(int i=0;i<gradeYear.size();i++){
            if(gradeYear.get(i).equals(thisYear+"年")){
                calendarYear.setSelected(i);
            }
        }

        for(int i=0;i<gradeMonth.size();i++){
            if(gradeMonth.get(i).equals(thisMonth+"月")){
                calendarMonth.setSelected(i);
            }
        }
        for(int i=0;i<gradeDay.size();i++){
            if(gradeDay.get(i).equals(thisDay%31+"日")){
                calendarDay.setSelected(i);
            }
        }


        //滚动选择事件
        calendarYear.setOnSelectListener(new CalendarView.onSelectListener() {
            @Override
            public void onSelect(String data) {
                year = data;

            }
        });
        calendarMonth.setOnSelectListener(new CalendarView.onSelectListener() {
            @Override
            public void onSelect(String data) {
                month = data;
            }
        });
        calendarDay.setOnSelectListener(new CalendarView.onSelectListener() {
            @Override
            public void onSelect(String data) {
                day = data;
            }
        });

        this.setCancelable(true);
        this.setCanceledOnTouchOutside(true);
    }


    public interface ClickListenerInterface {
        void doConfirm(Calendar calendar);
    }

    private ClickListenerInterface clickListenerInterface;

    public void setClicklistener(ClickListenerInterface clickListener) {
        clickListenerInterface = clickListener;
    }

    private class clickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.conform_tv:
                    clickListenerInterface.doConfirm(WeekUtils.getdate(year.replace("年","")+"-"+
                            month.replace("月","")+"-"+
                            day.replace("日",""),0));
                    dismiss();
                    break;
            }
        }
    }
}
